<template>
  <el-dialog class="form-dialog" title="微信模板" width="800px" :visible.sync="templateListVisible">
    <el-table
      :data="curList"
      style="width: 100%; overflow: auto"
    >

      <el-table-column
        prop="templateId"
        width="120"
        align="center"
        label="模板ID"
      />

      <el-table-column
        prop="userVersion"
        width="100"
        label="开发版本号"
      />

      <el-table-column
        prop="userDesc"
        label="开发备注"
      >
        <template slot-scope="scope">
          <div class="t-user-desc">{{ scope.row.userDesc }}</div>
        </template>
      </el-table-column>
      <el-table-column
        prop="developer"
        label="开发者"
      />

      <el-table-column
        prop="createTime"
        width="150"
        align="center"
        :formatter="convertTime"
        label="提交时间"
      />

      <el-table-column
        prop="option"
        align="center"
        width="80"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleSelect(scope.row)">选择</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      :current-page.sync="pageNum"
      :page-size="pageSize"
      layout="total, prev, pager, next"
      :total="total"
      @current-change="handleCurrentChange"
    />
  </el-dialog>
</template>

<script>
import dayjs from 'dayjs'
import { getTemplateList } from '@/api/wxapp'

export default {
  name: 'TemplateListDialog',
  data() {
    return {
      templateListVisible: false,
      resolve: null,
      reject: null,
      pageNum: 1,
      pageSize: 5,
      total: 0,
      templateList: [],
      curList: []
    }
  },
  methods: {
    getTemplateList() {
      getTemplateList().then((resp) => {
        this.templateList = resp.data
        if (!this.templateList || this.templateList.length === 0) {
          this.curList = []
          this.total = 0
        }
        this.total = this.templateList.length
        this.handleCurrentChange(1)
      })
    },
    handleCurrentChange(cur) {
      this.pageNum = cur
      const offset = (this.pageNum - 1) * this.pageSize
      this.curList = this.templateList.slice(offset, offset + this.pageSize)
    },

    handleSelect(template) {
      const resolve = this.resolve
      this.resolve = null
      this.reject = null
      resolve && resolve(template)
      this.templateListVisible = false
    },
    convertTime(row) {
      console.info(row.createTime)
      return dayjs.unix(row.createTime).format('YYYY-MM-DD HH:mm:ss')
    },
    show() {
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
        this.templateListVisible = true
        this.getTemplateList()
      })
    },
    close() {
      this.resolve = null
      this.reject = null
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .t-user-desc {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
  }
  .pagination {
    margin-top: 10px;
  }
  .form-dialog ::v-deep .el-dialog__body {
    padding: 20px 20px 20px 20px !important;
  }
</style>

